<template>
  <div ref="chart" style="width: 100%; height: 350px;"></div>
</template>

<script setup>
import {ref, onMounted, watch} from 'vue';
import * as echarts from 'echarts';


const props = defineProps({
  data: Array
});

const chart = ref(null);

const initChart = () => {
  if (chart.value && props.data) {
    const myChart = echarts.init(chart.value);

    const seriesData = props.data.map(item => ({
      name: item.title,
      value: item.count
    }));

    const option = {
      title: {
        text: '热门岗位分类',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'horizontal',
        left: 'center',
        bottom: '5%'
      },
      series: [
        {
          name: '分类',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '20',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: seriesData
        }
      ]
    };

    myChart.setOption(option);

    window.addEventListener('resize', () => {
      myChart.resize();
    });
  }
};

watch(() => props.data, initChart);

onMounted(initChart);
</script>

<style scoped>
/* 你可以在这里添加样式 */
</style>
